package cn.snorlaxzjt.LBSserver.service;

import cn.snorlaxzjt.LBSserver.push.SessionRepository;
import com.farsunset.cim.sdk.server.handler.CIMNioSocketAcceptor;
import com.farsunset.cim.sdk.server.model.CIMSession;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

    @Service
    public class CIMSessionService {

        @Resource
        private CIMNioSocketAcceptor nioSocketAcceptor;

        @Resource
        private SessionRepository sessionRepository;


        public void save(CIMSession session) {
            sessionRepository.save(session);
        }

        /*
         *
         * @param account 用户id
         * @return
         */

        public CIMSession get(String account) {

            CIMSession session = sessionRepository.get(account);

            if (session != null) {
                session.setSession(nioSocketAcceptor.getManagedSession(session.getNid()));
            }

            return session;
        }

        public void remove(String account) {
            sessionRepository.remove(account);
        }

        public List<CIMSession> list() {
            return sessionRepository.findAll();
        }

    }
